define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index   : () => {
            new Vue({
                el: '#configIndex',
                data() {
                    return {
                        activeName        : "basic",
                        configData        : {
                            basic   : [
                                {
                                    id        : 'health',
                                    title     : '平台信息',
                                    tip       : '配置平台基本信息',
                                    message   : '',
                                    icon      : 'shopro-icon',
                                    leaf      : '#6ACAA5',
                                    background: 'linear-gradient(180deg, #BAF0DD 0%, #51BC99 100%)',
                                    button    : {
                                        background: '#E0F1EB',
                                        color     : '#13986C'
                                    },
                                },
                                {
                                    id        : 'user',
                                    title     : '会员配置',
                                    tip       : '配置默认会员信息',
                                    message   : '',
                                    icon      : 'user-icon',
                                    leaf      : '#E0B163',
                                    background: 'linear-gradient(180deg, #FCE6B7 0%, #E9A848 100%)',
                                    button    : {
                                        background: '#F7EDDD',
                                        color     : '#B07318'
                                    },
                                },
                                {
                                    id        : 'withdraw',
                                    title     : '充值提现',
                                    tip       : '配置默认充值提现规则',
                                    message   : '',
                                    icon      : 'withdraw-icon',
                                    leaf      : '#EA6670',
                                    background: 'linear-gradient(180deg, #FCB7BE 0%, #D36068 100%)',
                                    button    : {
                                        background: '#F3DCDE',
                                        color     : '#D61226'
                                    },
                                },
                                {
                                    id        : 'order',
                                    title     : '商城配置',
                                    tip       : '配置默认商城规则',
                                    message   : '',
                                    icon      : 'order-icon',
                                    leaf      : '#6991E7',
                                    background: 'linear-gradient(180deg, #AFCBFD 0%, #5A7CCF 100%)',
                                    button    : {
                                        background: '#DBE2F6',
                                        color     : '#1B5EE7'
                                    },
                                },
                                {
                                    id        : 'health_plus',
                                    title     : '健康+配置',
                                    tip       : '配置健康+模块规则',
                                    message   : '',
                                    icon      : 'order-icon',
                                    leaf      : '#6991E7',
                                    background: 'linear-gradient(180deg, #AFCBFD 0%, #5A7CCF 100%)',
                                    button    : {
                                        background: '#DBE2F6',
                                        color     : '#1B5EE7'
                                    },
                                }
                            ],
                            platform: [
                                {
                                    id           : 'wxMiniProgram',
                                    title        : '小程序',
                                    tip          : '配置小程序',
                                    message      : '',
                                    icon         : 'wxMiniProgram-icon',
                                    leaf         : '#6962F7',
                                    background   : 'linear-gradient(180deg, #C1BFFF 0%, #6563C9 100%)',
                                    buttonMessage: '小程序设置',
                                    button       : {
                                        background: '#D8D8F1',
                                        color     : '#3932BF'
                                    },
                                }],
                        },
                        tipCloseFlag      : true,
                        notificationDialog: false,
                        notificationData  : [],
                        notificationForm  : {},
                        editIndex         : null,
                        editPlatform      : null
                    }
                },
                methods: {
                    tabClick(tab, event) {
                        this.activeName = tab.name;
                    },
                    operation(id, title) {
                        let that = this;
                        Fast.api.open("health/config/platform?type=" + id + "&tab=" + that.activeName + "&title=" + title, title);
                    },
                }
            })
        },
        platform: () => {
            new Vue({
                el: "#configPlatform",
                data() {
                    return {
                        must_delete : ['logo_arr', 'avatar_arr', 'image_arr', 'goods_poster_bg_arr', 'user_poster_bg_arr', 'groupon_poster_bg_arr', 'qrcode_arr', 'area_arr'],
                        platformData: {
                            health       : {
                                name    : '',
                                logo    : '',
                                logo_arr: [],
                            },
                            user         : {
                                avatar    : '',
                                avatar_arr: [],
                            },
                            withdraw     : {
                                withdraw_auto: 0,
                                service_fee  : 0,
                                min          : '',
                                max          : '',
                                perday_num   : 0,
                            },
                            order        : {
                                order_auto_close  : '',
                                order_auto_confirm: '',
                                // order_auto_comment: '',
                                // order_comment_content: '',
                                // user_reply: '0',
                                // goods: {
                                //     stock_warning: 0
                                // },
                                // invoice: {
                                //     enable: "1",
                                //     price_type: "goods_price"
                                // },
                                // refundCause: []
                            },
                            health_plus  : {
                                goal_step: 10000,
                            },
                            wxMiniProgram: {
                                name      : '',
                                avatar    : '',
                                qrcode    : '',
                                avatar_arr: [],
                                qrcode_arr: [],
                                app_id    : '',
                                secret    : '',
                            },
                        },
                        detailForm  : {},
                        type        : new URLSearchParams(location.search).get('type'),
                        tab         : new URLSearchParams(location.search).get('tab'),
                        title       : new URLSearchParams(location.search).get('title'),
                    };
                },
                mounted() {
                    this.operationData();
                },
                methods: {
                    operationData() {
                        this.detailForm = this.platformData[this.type]
                        if (Config.row) {
                            for (key in this.detailForm) {
                                if (Config.row[key]) {
                                    if (Config.row[key] instanceof Object) {
                                        for (inner in Config.row[key]) {
                                            if (Config.row[key][inner]) {
                                                this.detailForm[key][inner] = Config.row[key][inner]
                                            }
                                        }
                                    } else {
                                        this.detailForm[key] = Config.row[key]
                                    }
                                }
                            }
                        }
                    },
                    // 提交表单
                    submitFrom(type) {
                        let that = this;
                        if (type == 'yes') {
                            let submitData = JSON.parse(JSON.stringify(that.detailForm))
                            if (that.type == 'withdraw') {
                                submitData.service_fee = (Number(submitData.service_fee) / 100).toFixed(2)
                            }
                            that.must_delete.forEach(i => {
                                if (submitData[i]) {
                                    delete submitData[i]
                                }
                            });
                            that.$refs.detailForm.validate((valid) => {
                                if (valid) {
                                    Fast.api.ajax({
                                        url    : 'health/config/platform?type=' + that.type,
                                        loading: true,
                                        type   : 'POST',
                                        data   : {
                                            data : JSON.stringify(submitData),
                                            group: that.tab,
                                            title: that.title
                                        },
                                    }, function (ret, res) {
                                        Fast.api.close()
                                    })
                                }
                            })
                        } else {
                            Fast.api.close()
                        }
                    },
                    // 追加充值项
                    addTemplate() {
                        this.detailForm.recharge.moneys.push({
                            money: "",
                        });
                    },
                    // 删除充值项
                    deleteTemplate(index) {
                        this.detailForm.recharge.moneys.splice(index, 1);
                    },
                    // 选择资源文件
                    attachmentSelect(type, field) {
                        let that = this;
                        Fast.api.open("general/attachment/select?multiple=false", "选择", {
                            callback: function (data) {
                                switch (type) {
                                    case "image":
                                        that.detailForm[field] = data.url;
                                        that.detailForm[field + '_arr'] = data.url;
                                        break;
                                    // case "file":
                                    //     that.detailForm[field] = data.url;
                                    //     break;
                                    case "ssl":
                                        that.detailForm.system[field] = data.url;
                                        break;
                                }
                            }
                        });
                        return false;
                    },
                    // 删除图片
                    delImg(type, field) {
                        let that = this;
                        switch (type) {
                            case "image":
                                that.detailForm[field] = '';
                                that.detailForm[field + '_arr'] = [];
                                break;
                            case "file":
                                that.detailForm[field] = '';
                                break;
                        }
                    },
                    addRefundCause() {
                        this.detailForm.refundCause.push({
                            val: "",
                        });
                    },
                    deleteRefundCause(index) {
                        this.detailForm.refundCause.splice(index, 1);
                    },
                }
            })
        }
    }
    return Controller;
});